using System.IO;
using Core;
using UnityEditor;
using UnityEngine;

public static class SplitProj
{
    private static GameCollection collection;

    [MenuItem("Assets/Mini Tools/Proj/Split Game")]
    private static void Main()
    {
        collection = new GameCollection();
        collection.Load();

        var games = collection.GameList;
        foreach (var game in games)
        {
            Debug.Log($"开始拆分 Game {game.Name}");
            Process(game);
            Debug.Log("=====================================");
        }

        EditorUtility.ClearProgressBar();
        Debug.LogError("Split Game 处理完成");
    }

    private static void Process(GameCollection.GameVO gameVO)
    {
        var folders = gameVO.Folders;
        var files = gameVO.Files;

        var folderCount = folders.Count;
        var fileCount = files.Count;
        var totalCount = folderCount + fileCount;

        // System.Environment.CurrentDirectory

        var gameConf = collection.GetGameConf(gameVO.Name);
        var srcRoot = Path.Combine(Application.dataPath, "../");
        var tgtRoot = Path.Combine(Application.dataPath, $"../../game_{gameConf.Name.ToLower()}/");

        if (Directory.Exists(tgtRoot) == false)
        {
            Debug.LogError($"工程[{gameConf.Desc}]不存在，Path={tgtRoot}");
            return;
        }

        for (var i = 0; i < folders.Count; i++)
        {
            var folder = folders[i];
            var srcPath = Path.Combine(srcRoot, folder.Path);
            var tgtPath = Path.Combine(tgtRoot, folder.Path);
            Debug.Log($"拷贝 Folder {folder.Path}");
            EditorUtility.DisplayProgressBar($"{gameVO.Name}", $"正在拷贝Folder:{folder.Path}", i * 1.0f / totalCount);
            Utils.File.CopyFolder(srcPath, tgtPath);
        }

        for (var i = 0; i < files.Count; i++)
        {
            var file = files[i];

            var srcPath = Path.Combine(srcRoot, file.Path);
            var tgtPath = Path.Combine(tgtRoot, file.Path);
            Debug.Log($"拷贝 File {srcPath}");

            System.IO.File.Copy(srcPath, tgtPath, true);

            EditorUtility.DisplayProgressBar($"{gameVO.Name}", $"正在拷贝File:{file.Path}", (i + folderCount) * 1.0f / totalCount);
        }
    }
}